﻿@using Telerik.Web.Mvc
@using Telerik.Web.Mvc.UI
@using Presentation.Web.Admin
@model  IEnumerable<Infrastructure.Crosscutting.Authorize.Dto.MvcActionDto>
@{
    ViewBag.subtitle = "未安装权限";
    ViewBag.title = "未安装权限";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@{Html.Telerik().Grid(Model)
    .Name("UninstallPermissionGrid")
    .ToolBar(toolBar => toolBar.Template(
    @<text>
    @{
        if (Html.HasUiPermission(this.ViewContext, "searchuninstall"))
        {
        <label class="customer-label" for="Customers-input">
            控制器名:
        </label>
        @Html.Partial("_ControllerList")
        <label class="customer-label" for="Customers-input">
            权限系统名:
        </label>
        <input type="text" id="searchPermission" value="权限系统名" data-autocomplete="/permission/searchuninstall" />
        <input type='button' value='我要搜索' class='t-button' onclick='searchChange()' />
        }
        if (Html.HasUiPermission(this.ViewContext, "mulinstall"))
        { 
        <a href='javascript:;' class='t-button' onclick='mulInstall()'>安装选择项</a>
        }
        <a href="/permission/" style="float: right;" class="t-button">返回权限列表</a>
    }
    </text>))
    .Columns(columns =>
    {
        columns.Template(@<text><input name="selectRow" type="checkbox" value="@item.ActionName|@item.ControllerName " title="checkedRecords" /></text>)
        .HeaderTemplate(@<text><input type="checkbox" title="选择所有" id="checkAllRecords" /></text>)
        .Width(50).HeaderHtmlAttributes(new { style = "text-align:center" }).HtmlAttributes(new { style = "text-align:center" })
        .ClientTemplate("<input type=\"checkbox\" title=\"check all records\" name = 'selectRow' value = '<#= ActionName #>|<#= ControllerName#>'/>");
        columns.Bound(o => o.ActionId).Width(50).Title("系统Id").Hidden();
        columns.Bound(o => o.ActionName).Width(200).Title("权限系统名");
        columns.Bound(o => o.ActionRemark).Width(200).Title("权限名描述");
        columns.Bound(o => o.ControllerClassId).Width(100).Title("控制器分类Id").Hidden();
        columns.Bound(o => o.ControllerClassName).Width(100).Title("控制器分类名").Hidden();
        columns.Bound(o => o.ControllerId).Width(300).Title("所属控制器Id").Hidden();
        columns.Bound(o => o.ControllerName).Width(3000).Title("所属控制器系统名").Hidden(false);
        columns.Bound(o => o.ControllerRemark).Width(3000).Title("所属控制器名");
    })
    .ClientEvents(clientEvents => clientEvents.OnDataBinding("dataBinding").OnComplete("bindComplete"))
    .DataKeys(key => key.Add(e => e.ActionName))//添加主键
    .DataBinding(dataBinding => dataBinding.Ajax().Select("ajaxuninstall", "permission"))
    .Pageable(page => page.PageSize(15))
    .Groupable()
    .Render();
}
<script type="text/javascript">
    function searchChange() {
        $("#UninstallPermissionGrid").data("tGrid").rebind();
    }

    function dataBinding(args) {
        var searchKeyStr = $("#searchPermission").val();
        var controllerIdStr = $("#controllerId").val();
        if (searchKeyStr == '权限系统名') {
            searchKeyStr = '';
        }
        args.data = $.extend(args.data, { searchKey: searchKeyStr, controllerId: controllerIdStr });
    }

    function mulInstall() {
        var delStr = $(':checked');
        if (delStr.length < 1) {
            alert('亲，您没有选择任何记录.');
            return;
        }
        jQuery.post('/permission/mulInstall', delStr, function (res) {
            if (res.success) {
                setTimeout(function () {
                    $("#UninstallPermissionGrid").data("tGrid").rebind();
                    $("#checkAllRecords").attr("checked", false);
                }, 1000);
            }
            else {
                alert(res.message);
            }
        });
    }

    function bindComplete(e) {
        $("input:checkbox").attr("checked", false);
        var success = e.response.success;
        if (success != null && success != undefined) {
            alert(e.response.message);
            window.location.reload();
        }
    }

    $(function () {
        $("input:checkbox").attr("checked", false);
        $('#checkAllRecords').click(function checkAll() { $("#UninstallPermissionGrid tbody input:checkbox").attr("checked", this.checked); });
        $(":input[data-autocomplete]").each(function () {
            $(this).autocomplete({
                source: $(this).attr("data-autocomplete")
            }
            );
        });
        $("#searchPermission").focus(function () {
            if (this.value == "权限系统名") {
                $("#searchPermission").val("");
            }
        }).blur(function () {
            if (this.value == "") {
                $("#searchPermission").val("权限系统名");
            }
        });
    });
</script>
